package com.itmk.business.product.controller;

import com.itmk.business.product.entity.Product;
import com.itmk.business.product.entity.PurchasingPrice;
import com.itmk.business.product.service.ProductService;
import com.itmk.business.product.service.PurchasingPriceService;
import com.itmk.result.DataResult;
import com.itmk.utils.PageUtil;
import com.itmk.vo.req.ProductReqVO;
import com.itmk.vo.resp.PageRespVO;
import com.itmk.vo.resp.PurchasingPriceRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/product")
@Api(tags = "商品管理")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private PurchasingPriceService priceService;
    @ApiOperation(value = "获取商品列表")
    @RequestMapping(value = "/getproductlist", method = RequestMethod.POST)
    public DataResult getProductList(@RequestBody ProductReqVO vo) {
        List<Object> result = productService.getProductListsByProcedure(vo);
        PageRespVO<Object> pageRespVO= PageUtil.getPageVo(result);
        return DataResult.success(pageRespVO);
    }

//    专门为编辑文章里的管理产品用
    @ApiOperation(value = "获取不分页全部商品列表")
    @RequestMapping(value = "/getallproductlist", method = RequestMethod.POST)
    public DataResult getAllProductList() {
        List<Map<String,Object>> mapResult=productService.getAllProductList();
        return DataResult.success(mapResult);
    }


    @ApiOperation(value = "新增商品")
    @RequestMapping(value = "/addproduct", method = RequestMethod.POST)
    public DataResult addProductCategory(@RequestBody Product p) {

        Boolean b = this.productService.save(p);
        if (b) {
            return DataResult.success();
        } else {
            return DataResult.getResult(300100, "新增失败!");
        }

    }

    @ApiOperation(value = "更新商品")
    @RequestMapping(value = "/updateproduct", method = RequestMethod.POST)
    public DataResult updateProductCategory(@RequestBody Product p) {

        Boolean b = this.productService.updateById(p);
        if (b) {
            return DataResult.success();
        } else {
            return DataResult.getResult(300100, "更新失败!");
        }

    }

    @ApiOperation(value = "根据Id获取商品")
    @RequestMapping(value = "/getproductbyid", method = RequestMethod.POST)
    public DataResult getProductById(@RequestBody Product p) {
        Product product = productService.getById(p.getProductId());
        return DataResult.success(product);

    }

    @ApiOperation(value = "根据Id删除商品")
    @RequestMapping(value = "/deleteproductbyid", method = RequestMethod.POST)
    public DataResult deleteProdectById(@RequestBody Product p) {
        Boolean b = productService.removeById(p.getProductId());
        if (b) {
            return DataResult.success();
        } else {
            return DataResult.getResult(300100, "删除失败!");
        }


    }

    @ApiOperation(value = "获取价格列表")
    @RequestMapping(value = "/getpricelist", method = RequestMethod.POST)
    public DataResult getPriceList(@RequestBody ProductReqVO vo) {
        List<Object> result = priceService.getPriceList(vo);
        PageRespVO<Object> pageRespVO= PageUtil.getPageVo(result);
        return DataResult.success(pageRespVO);
    }

    @ApiOperation(value = "新增价格")
    @RequestMapping(value = "/addproductprice", method = RequestMethod.POST)
    public DataResult addPrice(@RequestBody PurchasingPrice p) {
        Boolean b=this.priceService.save(p);
        if(b){
            return DataResult.success();
        }else{
            return DataResult.getResult(300100, "新增价格失败!");
        }
    }

    @ApiOperation(value = "根据Id商品价格")
    @RequestMapping(value = "/getpurchasingprice", method = RequestMethod.POST)
    public DataResult getPurchasingPriceById(@RequestBody PurchasingPrice p) {
        PurchasingPriceRespVO priceResult = priceService.getPriceDetailById(p.getId());
        return DataResult.success(priceResult);

    }
    @ApiOperation(value = "根据Id删除价格")
    @RequestMapping(value = "/deletepurchasingprice", method = RequestMethod.POST)
    public DataResult deletePurchasingPriceById(@RequestBody PurchasingPrice p) {
        Boolean b= priceService.removeById(p.getId());
        if(b){
            return DataResult.success();
        }else{
            return DataResult.getResult(300100, "删除价格失败!");
        }

    }

    @ApiOperation(value = "根据Id更新价格")
    @RequestMapping(value = "/updatepurchasingprice", method = RequestMethod.POST)
    public DataResult updatePurchasingPriceById(@RequestBody PurchasingPrice p) {
        Boolean b= priceService.updateById(p);
        if(b){
            return DataResult.success();
        }else{
            return DataResult.getResult(300100, "更新价格失败!");
        }

    }
}
